package com.graduation.table.authority.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.graduation.base.Result;
import com.graduation.table.administrators.entity.Administrators;
import com.graduation.table.authority.entity.Authority;
import com.graduation.table.authority.service.IAuthorityService;
import com.graduation.table.company.entity.Company;
import com.graduation.util.PageUtil;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Description: 权限
 * @Author: jeecg-boot
 * @Date:   2022-04-13
 * @Version: V1.0
 */
@Slf4j
@Api(tags="权限")
@RestController
@RequestMapping("/authority")
public class AuthorityController {
	@Autowired
	private IAuthorityService authorityService;
	
	/**
	 * 分页列表查询
	 *
	 * @param authority
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@ApiOperation(value="权限-分页列表查询", notes="权限-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(Authority authority,
								   String search,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		LambdaQueryWrapper<Authority> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper
				.like(!StringUtils.isEmpty(search), Authority::getUri,search)
		;
		List<Authority> list = authorityService.list(queryWrapper);
		PageUtil pageUtil = PageUtil.doPage(pageNo,pageSize,list);
		return Result.OK(pageUtil);
	}
	
	/**
	 * 添加
	 *
	 * @param authority
	 * @return
	 */
	@ApiOperation(value="权限-添加", notes="权限-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody Authority authority) {
		authorityService.save(authority);
		return Result.OK("添加成功！");
	}
	
	/**
	 * 编辑
	 *
	 * @param authority
	 * @return
	 */
	@ApiOperation(value="权限-编辑", notes="权限-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody Authority authority) {
		authorityService.updateById(authority);
		return Result.OK("编辑成功!");
	}
	
	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="权限-通过id删除", notes="权限-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		authorityService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@ApiOperation(value="权限-批量删除", notes="权限-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.authorityService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功！");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="权限-通过id查询", notes="权限-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		Authority authority = authorityService.getById(id);
		return Result.OK(authority);
	}


}
